package com.qc.searchBug.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.qc.searchBug.pojo.BugInfo;
import com.qc.searchBug.pojo.UserInfo;
import com.qc.searchBug.services.BugInfoServices;
import com.qc.searchBug.util.MyCon;

@Controller
@RequestMapping(value="/bugInfoController")
public class BugInfoController {
	@Autowired
	private BugInfoServices bugInfoBiz;
	@RequestMapping(value="/addBug")
	public String addBug(BugInfo bugInfo,HttpSession session){
		UserInfo userInfo=(UserInfo) session.getAttribute(MyCon.USER_INFO);
		bugInfo.setAddpersonid(userInfo.getUserid());
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("solution", bugInfo.getSolution());
		map.put("description", bugInfo.getDescription());
		map.put("Addpersonid", bugInfo.getAddpersonid());
		int i=bugInfoBiz.addBug(map);
		if(i>0){
			return "addBugInfo";
		}
		return "";
	}
	@ResponseBody
	@RequestMapping(value="/ajaxFindBug/{str}",produces="text/html;charSet=utf-8" )
	public String ajaxFindBug(@PathVariable("str") String str,Map<String,Object> map ){
		String str1=null;
		try {
			str1=new String(str.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(str1!=null||str1!=""){
			map.put("description", str1);
			List<Map<String,Object>> list = bugInfoBiz.findByAjax(map);
			if(null!=list){
				return JSON.toJSONString(list);
			}
		}
		return "";
	}
	
	
	@RequestMapping(value="/findByDescription")
	public String findByDescription(@RequestParam String description,@RequestParam(defaultValue="1") int indexPage,@RequestParam(defaultValue="10") int pageSize,Map<String,Object> map){
		String description1=null;
		try {
			description1=new String(description.getBytes("ISO-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		List<Map<String,Object>> list=bugInfoBiz.findByDescription(description1,indexPage,pageSize);
		int pageNumber=10;
		
		//遍历次数
		List<String> list1=new ArrayList<String>();
		for(int i=0; i < pageNumber ;i++){
			list1.add(i+"");
		}
		map.put("description", description1);
		map.put("list", list);
		map.put("pageNumber",list1);
		return "bugInfo";
	}
	
	@RequestMapping(value="/findByBugId")
	public String findByBugId(@RequestParam int bugId,Map<String,Object> map){
		Map<String,Object> buInfoDetial=bugInfoBiz.findByBugId(bugId);
		if(buInfoDetial!=null){
			int queriesnumber = (Integer) buInfoDetial.get("queriesnumber")+1;
			bugInfoBiz.updateQueriesnumber(bugId,queriesnumber);
			map.put("buInfoDetial", buInfoDetial);
			return "bugInfoDetial";
		}
		return "BugInfo";
		
	}
	@RequestMapping(value="/findBugByUserId")
	public String findBugByUserId (@RequestParam int userId,@RequestParam(defaultValue="1") int indexSize, Map<String,Object> map){
		int pageSize=5;
		List<Map<String,Object>> list = bugInfoBiz.findBugByUserId(userId,indexSize,pageSize);
		int stateNumber=bugInfoBiz.findPageNumberByUserId(userId);//查询总共多少条数据
		int PageNumber=0;
		if(stateNumber%pageSize==0){
			PageNumber=stateNumber/pageSize;
		}else if(stateNumber%pageSize!=0){
			PageNumber=(stateNumber/pageSize)+1;
		}
		List<String> list1 =new ArrayList<String>();
		for(int i=0;i<PageNumber;i++){
			list1.add(i+"");
		}
		if(list!=null){
			map.put("pageNumber", list1);
			map.put("list", list);
			return "back-stage/userIndex";
		}
		return "";
	}
	/**
	 * 根据BugId删除Bug信息
	 */
	@RequestMapping(value="/deleteByBugId")
	public String deleteByBugId(@RequestParam int bugId){
		int i=bugInfoBiz.deleteByBugId(bugId);
		if(i>0){
			System.out.println("删除成功");
			return "back-stage/userIndex";
		}
		return "";
		
	}
}






